package com.allwees.bs.module.oms.service.impl;

import com.allwees.bs.core.model.order.wf.order.EOrderItemAction;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.lms.dao.LogisticsDao;
import com.allwees.bs.module.lms.dao.LogisticsWaybillDao;
import com.allwees.bs.module.lms.entity.Logistics;
import com.allwees.bs.module.lms.entity.LogisticsWaybill;
import com.allwees.bs.module.lms.service.LogisticsTrackService;
import com.allwees.bs.module.oms.dao.OmsOrderDao;
import com.allwees.bs.module.oms.dao.OmsOrderItemDao;
import com.allwees.bs.module.oms.dao.OmsShippingLogDao;
import com.allwees.bs.module.oms.dao.OrderDao;
import com.allwees.bs.module.oms.dao.OrderItemDao;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.entity.OmsShippingLog;
import com.allwees.bs.module.oms.entity.OrderItem;
import com.allwees.bs.module.oms.service.BaseOmsOrderItemService;
import com.allwees.core.common.constant.EStatus;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Set;

/**
 * @author yangylsky
 * @since 2020-01-09
 */

public abstract class BaseOmsOrderItemServiceImpl extends BoServiceImpl<OmsOrderItemDao, OmsOrderItem> implements BaseOmsOrderItemService {

    @Autowired
    protected OmsOrderDao omsOrderDao;

    @Autowired
    protected OrderItemDao orderItemDao;

    @Autowired
    protected OrderDao orderDao;

    @Autowired
    protected OmsOrderItemDao omsOrderItemDao;

    @Autowired
    protected OmsShippingLogDao omsShippingLogDao;

    @Autowired
    protected LogisticsWaybillDao logisticsWaybillDao;

    @Autowired
    protected LogisticsDao logisticsDao;

    @Autowired
    protected LogisticsTrackService logisticsTrackService;

    protected void _doShipped(String orderItemUuid, String logisticsUuid, String expressCode, Date shippingAt, Logistics logistics) {
        OmsOrderItem omsOrderItem = new OmsOrderItem();
        omsOrderItem.setUuid(orderItemUuid);
        omsOrderItem.setEStatus(EStatus.OMS_SHIPPED);
        omsOrderItem.setShippingAt(shippingAt);
        omsOrderItem.setLogisticsUuid(logisticsUuid);
        omsOrderItem.setExpressCode(expressCode);
        omsOrderItem.setLogisticsNameEn(logistics.getNameEn());
        omsOrderItem.setLogisticsNameZh(logistics.getNameZh());
        updateByUuid(omsOrderItem);

        _doOrderItemShipped(orderItemUuid, expressCode, shippingAt);
    }

    protected void _doOrderItemShipped(String orderItemUuid, String expressCode, Date shippingAt) {
        OrderItem orderItem = orderItemDao.findByUuid(orderItemUuid);
        orderItem.signal(EOrderItemAction.MERCHANT_SHIPPED);
        orderItem.setExpressCode(expressCode);
        orderItem.setShippingAt(shippingAt);
        orderItemDao.updateByUuid(orderItem, orderItemUuid);
    }

    protected void _saveShippedLog(Set<String> orderItemUuids, String logisticsUuid, String expressCode) {
        OmsShippingLog omsShippingLog = new OmsShippingLog(StringUtils.join(orderItemUuids.toArray(new String[0]),"|"), logisticsUuid, expressCode);
        omsShippingLogDao.insert(omsShippingLog);
    }

    protected void _saveLogisticsWaybill(String orderItemUuid, String orderUuid, String expressCode, Date shippingAt, String carrireCode) {
        LogisticsWaybill waybill = new LogisticsWaybill();
        waybill.setOrderItemUuid(orderItemUuid);
        waybill.setOrderUuid(orderUuid);
        waybill.setShippingDate(shippingAt);
        waybill.setExpressCode(expressCode);
        waybill.setCarrierCode(carrireCode);
        logisticsWaybillDao.insert(waybill);
    }

    /**
     * 创建关注物流单号
     */
    protected void _saveTrackingLogisticsWaybill(String expressCode, String channelCode){
        if(!channelCode.equals("IMILE")) {
            logisticsTrackService.createPostOrder(expressCode, channelCode);
        }
    }

    @Override
    public boolean allInStatus(String orderUuid, EStatus status) {
        Integer allCount = baseMapper.queryCountByOrderUuid(orderUuid);
        Integer inStatusCount = baseMapper.queryCountByOrderUuidAndStatus(orderUuid, status);
        return allCount.equals(inStatusCount);
    }
}
